<?php
namespace Hanson\MyVbot\Amazon\Comment;

use Hanson\Vbot\Foundation\Vbot;
use QL\QueryList;
/**
 * Created by PhpStorm.
 * User: chenishr
 * Date: 2018/7/12
 * Time: 10:26 PM
 */

class Comments {
    static $data = [];
    public static function get_comments($url,$page){
        ini_set('memory_limit', '512M');
        set_time_limit(0);

        //$url = "https://www.amazon.com/Otium-Soar-Bluetooth-Headphones-Relaxation/product-reviews/B07DGP157Z/ref=cm_cr_arp_d_viewopt_srt?ie=UTF8&reviewerType=all_reviews&sortBy=recent&pageNumber=1";
		if(!$url){
			$url = "https://www.amazon.com/Transmitter-Otium-Bluetooth-Wireless-Modulator/product-reviews/B01DZKERVY/ref=cm_cr_arp_d_viewopt_rvwer?ie=UTF8&reviewerType=all_reviews&sortBy=recent&pageNumber=1&filterByStar=all_stars";
		}
        $date = "2010-01-01";

        // 验证 URL 是否正确
        if(false === strpos($url,'reviewerType=all_reviews')){
            return false;
        }

        if(!$page) {
            $page = 0;
        }

        $reg = '/(pageNumber=)\d+/';

        try{
            do{
                $page ++;
                $url = preg_replace($reg,'${1}' . $page,$url);

            }while(self::_get_comments($url,$page,$date));
        }catch (\Exception $e){
            return false;
        }

        $fileName = "comments-". date("Y-m-d~H:i:s") . "-" .rand(100,999) . '.csv';

        $file = fopen("/tmp/" . $fileName ,"w");

        foreach (self::$data as $line)
        {
            fputcsv($file,$line,',','"');
        }

        fclose($file);

        self::$data = [];
        return $fileName;
    }
    /**
     * 抓取评论信息，保存到内存
     * @param $url
     * @param $page
     * @return bool
     */
    private static function _get_comments($url, $page, $date) {
        echo "fetching: " . $url . "\n";
        // 计数
        $index = ($page - 1) * 10;

        $html = self::_getSslPage($url);
        $rules = array(
            'comments' => array(' .review', 'html'),
        );
        $data = QueryList::html($html)->rules($rules)->query()->getData();
        unset($html);

        // 抓取失败
        if (!$data || empty($data[0]['comments'])) {
            return false;
        }

        $parseUrl = parse_url($url);

        foreach ($data as $val) {
            $subRules = array(
                'title' => array('.review-title', 'text'),
                'starts' => array('.review-rating > .a-icon-alt', 'text'),
                'author' => array('.author', 'text'),
                'author_url' => array('.author', 'href'),
                'comment' => array('.review-text', 'text'),
                'comment_url' => array('.review-title', 'href'),
                'date' => array('.review-date', 'text'),
            );

            $comment = QueryList::html($val['comments'])->rules($subRules)->query()->getData();
            $tmpData = $comment->all()[0];

            // 超出时间段的则返回
            if (strtotime(str_replace('on', '', $tmpData['date'])) < strtotime($date)) {
                return false;
            }

            /*
            // 排除 4 星 5 星
            if (strpos($tmpData['starts'], '5.0 out') !== false
                || strpos($tmpData['starts'], '4.0 out') !== false) {
                continue;
            }
            */

            // 补全 url
            $tmpData['author_url'] = $parseUrl['scheme'] . "://" . $parseUrl['host'] . $tmpData['author_url'];
            $tmpData['comment_url'] = $parseUrl['scheme'] . "://" . $parseUrl['host'] . $tmpData['comment_url'];

            $index++;

            $appendData = [
                'index' => $index,
                'page' => $page,
            ];
            $tmpData = array_merge($appendData, $tmpData);

            self::$data[] = $tmpData;
			print_r($tmpData);

            unset($tmpData);
            unset($comment);
        }

        // 不够十个则抓取完毕
        if(count($data) < 10){
            return false;
        }

        return true;
    }

    /**
     * 执行网络请求
     * @param $url
     * @return mixed
     */
    private static function _getSslPage($url) {
        $userAgent = 'Mozilla/5.0 (Windows NT 5.1; rv:31.0) Gecko/20100101 Firefox/31.0';

        $ch = curl_init();
        curl_setopt( $ch, CURLOPT_USERAGENT, $userAgent );
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_REFERER, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        $result = curl_exec($ch);
        curl_close($ch);
        return $result;
    }
}
